#!/usr/bin/env python

import os, sys
from stars_class import Stars

def obj_set_rank ( liwe, id_obj, module = '', rank = 0, id_site = 0 ):
	"""
	:INPUT:
		- id_obj	SAFE_DB_NUMBER
		- module	SAFE_DB_STRING
		- rank		SAFE_DB_NUMBER
		- id_site	SAFE_DB_NUMBER
	"""
	
	s = Stars ( liwe )
		
	id_rank = s.add_ranking ( id_obj = id_obj, module = module, rank = rank, id_site = id_site )

	return { "id_rank" : id_rank }

def list ( liwe, id_obj = "", module = "", id_site = 0 ):
	"""
	:INPUT:
		- id_obj	- ID object to list SAFE_DB_STRING
		- module	- SAFE_DB_STRING
		- id_site	- SAFE_NUMBER
	"""
	s = Stars ( liwe )
	rows = s.list ( id_obj = id_obj, module = module, id_site = id_site )
	if not rows: rows = []

	return { "ranking" : rows }

def obj_rank ( liwe, id_obj = "", module = "", id_site = 0 ):
	"""
	:INPUT:
		- id_obj	- ID object to list SAFE_DB_STRING
		- module	- SAFE_DB_STRING
		- id_site	- SAFE_NUMBER
	"""
	import math

	s = Stars ( liwe )
	user_rank = 0
	urank = s.user_rank ( id_obj = id_obj, module = module, id_site = id_site )
	if urank:
		user_rank = urank [ 'rank' ];
 
	res = s.obj_rank ( id_obj = id_obj, module = module, id_site = id_site )

	if not res:
		return { "obj_rank" : 0, "tot_users" : 0, "user_rank" : user_rank }

	obj_rank = math.ceil ( res [ 'sum_rank' ] / res [ 'tot_users' ] )
	return { "obj_rank" : obj_rank, "tot_users" : tot_users, "user_rank" : user_rank }

def get_templates ( liwe ):
	"""
	:INPUT:
	"""
	s = Stars ( liwe )
        return { "templates" : s.get_templates () }

